package com.example.cookbook;

import androidx.annotation.NonNull;

import com.example.cookbook.bean.RecipeBean;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

//公共类NetUtil，用于封装网络请求操作。
public class NetUtil {
    //内部接口OnRecipeQueriedCallback，用于处理菜谱查询的回调。
    public interface OnRecipeQueriedCallback {
        void onSuccess(List<RecipeBean> recipes);
        void onFailure(Exception e);
    }
    //通过使用okHttp异步调用菜谱接口，并传入菜谱类型和回调函数
    //定义一个静态方法queryRecipe，接受一个标签tag和一个回调callback作为参数，用于查询菜谱。
    public static void queryRecipe(String tag, final OnRecipeQueriedCallback callback) {

        OkHttpClient okHttpClient = new OkHttpClient();

        Request request = new Request.Builder().url(String.format("https://route.showapi.com/1164-1?showapi_appid=1642209 &type= %s&showapi_sign=79f2ebd76d9945d5bd18739eabd504c9" ,tag)).build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                callback.onFailure(e);
            }
            @Override   //在请求成功时调用的回调方法onResponse，接收一个Call对象和一个Response对象。
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                try {
                    String jsonStr = response.body().string();
                    JsonObject jsonObject = JsonParser.parseString(jsonStr).getAsJsonObject();
                    List<RecipeBean> recipes = new Gson().fromJson(jsonObject.getAsJsonObject("showapi_res_body")
                            .getAsJsonArray("datas"), new TypeToken<List<RecipeBean>>(){}.getType());
                    callback.onSuccess(recipes);
                } catch (Exception e) {
                    callback.onFailure(e);
                }
            }
        });
    }

    public static void queryRecipe_1(String type, String id, final OnRecipeQueriedCallback callback) {

        OkHttpClient okHttpClient = new OkHttpClient();

        Request request = new Request.Builder().url(String.format("https://route.showapi.com/1164-1?showapi_appid=1642209&showapi_sign=79f2ebd76d9945d5bd18739eabd504c9&cpName=&id=%s&maxResults=&type=%s&page=" ,id,type)).build();


        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                callback.onFailure(e);
            }
            @Override   //在请求成功时调用的回调方法onResponse，接收一个Call对象和一个Response对象。
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                try {
                    String jsonStr = response.body().string();
                    JsonObject jsonObject = JsonParser.parseString(jsonStr).getAsJsonObject();
                    List<RecipeBean> recipes = new Gson().fromJson(jsonObject.getAsJsonObject("showapi_res_body")
                            .getAsJsonArray("datas"), new TypeToken<List<RecipeBean>>(){}.getType());
                    callback.onSuccess(recipes);
                } catch (Exception e) {
                    callback.onFailure(e);
                }
            }
        });
    }
}
